<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class UploadController extends Controller
{
    /**
     * 图片上传
     * @param Request $request
     * @return JsonResponse
     */
    public function image(Request $request): JsonResponse
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:10240', // 最大10MB
        ]);
        $storagePath = 'images/'.date('Ym').'/'.date('d');
        $filePath= $request->file('image')->store($storagePath);
        return apiResponse(data: [
            'path'=>$filePath,
            'fullPath' => asset($filePath)
        ]);
    }

    /**
     * 文件上传
     * @param Request $request
     * @return JsonResponse
     */
    public function file(Request $request): JsonResponse
    {
        $request->validate([
            'file' => 'required|mimes:jpeg,png,jpg,gif,svg,pdf,doc,docx,xlsx|max:10240', // 最大10MB
        ]);
        $storagePath = 'files/'.date('Ym').'/'.date('d');
        $filePath= $request->file('file')->store($storagePath);
        return apiResponse(data: [
            'path'=>$filePath,
            'fullPath' => asset($filePath)
        ]);
    }
}
